package sharetime.playlist.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import sharetime.constants.Messages;
import sharetime.playlist.domain.Playlist;
import sharetime.playlist.service.IPlaylistManager;
import sharetime.playlist.service.PlaylistValidator;
import sharetime.user.domain.UserData;
import sharetime.web.controllers.BaseController;

/**
 * @author zhangyf
 */
@Controller
@RequestMapping("/editplaylist")
public class EditPlaylistController extends BaseController {

	private static final Logger logger = LoggerFactory
			.getLogger(EditPlaylistController.class);

	@Autowired
	@Qualifier("playlistManager")
	private IPlaylistManager playlistManager;

	@RequestMapping(method = RequestMethod.GET)
	public String index(@RequestParam
	long id, Model model) {
		// 在什么地方做权限控制呢？
		Playlist playlist = playlistManager.getPlaylistById(id);
		model.addAttribute("playlist", playlist);
		return null;
	}

	/**
	 * 
	 * @param playlist
	 * @param br
	 *            BindingResult 需要紧随ModelAttribute参数之后
	 * @param model
	 * @param response
	 */
	@RequestMapping(method = RequestMethod.POST, params = { "!_method" })
	public void create(@ModelAttribute("playlist")
	Playlist playlist, BindingResult br, Model model,
			HttpServletResponse response) {

		new PlaylistValidator().validate(playlist, br);

		if (br.hasErrors()) {
			model.addAttribute("msg", "ERROR");
			renderJson(response, model, null, null);
		} else {
			UserData ud = userManager.getUserData(getRemoteUserId());
			playlist.setOwner(ud);
			logger.debug("tag size: " + playlist.getTags().size());
			playlistManager.createPlaylist(playlist);

			String[] excludeAttributes = { "*.source" };
			String[] includeAttributes = { "*.tags" };

			model.addAttribute("msg", Messages.OK);
			model.addAttribute("playlist", playlist);

			renderJson(response, model, excludeAttributes, includeAttributes);
		}
	}

	@RequestMapping(method = RequestMethod.POST, params = { "_method=PUT" })
	public String update(@ModelAttribute("playlist")
	Playlist playlist, Model model) {
		playlistManager.updatePlaylist(playlist);
		model.addAttribute("playlist", playlist);
		model.addAttribute("msg", Messages.OK);
		return null;
	}

	@RequestMapping(method = RequestMethod.POST, params = { "_method=DELETE" })
	public void delete(@RequestParam("id")
	long id, HttpServletResponse response) {
		playlistManager.removePlaylist(id);
		renderText(response, "{msg : 'OK'}");
	}

	@Override
	protected void doInitBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) {
		binder.registerCustomEditor(List.class, "tags",
				new TagsPropertyEditor());
	}
}
